/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cratos.xxgramoss.user;

import com.cratos.xxgramoss.group.GroupDetailOssService;
import com.cratos.xxgram.platf.base.BaseService;
import com.cratos.xxgram.platf.complaint.ComplaintRecord;
import javax.annotation.Resource;
import org.redkale.net.http.RestService;
import org.redkale.source.*;
import org.redkale.util.*;
import static com.cratos.xxgramoss.base.Services.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.redkale.net.http.*;
import org.redkale.service.RetResult;

/**
 *
 * @author alter
 */
@RestService(comment = "举报管理", moduleid = MODULE_COMPLAINT)
public class ComplaintRecordOssService extends BaseService {

    @Resource(name = "platf")
    protected DataSource platfSource;
    
    @Resource
    protected UserOssService userService;
    
    @Resource
    protected GroupDetailOssService groupService;

    @RestMapping(auth = true, actionid = ACTION_QUERY, comment = "查询举报列表")
    public RetResult<Sheet<ComplaintRecord>> queryComplaintList(ComplaintRecordBean bean, Flipper flipper) {
        Set<Integer> set = source.queryColumnSet("targetid",ComplaintRecord.class,bean);
        Sheet<ComplaintRecord> sheet = new Sheet<>();
        List complaints = new ArrayList<>();
        for(int id:set) {
           ComplaintRecord complaint = source.find(ComplaintRecord.class, "targetid",id);
           ComplaintRecord record = new ComplaintRecord();
           if(complaint.getComplaintmodule()==ComplaintRecord.USERINFO_MODULE){
               record.setTargetname(userService.findUserName(complaint.getTargetid()));
           }else{
               record.setTargetname(groupService.findGroupName(complaint.getTargetid()));
           }
           record.setTargetid(id);
           int times = platfSource.getNumberResult(ComplaintRecord.class, FilterFunc.COUNT,0,"complaintuserid", FilterNode.create("targetid", FilterExpress.EQUAL, id)).intValue();
           record.setTimes(times);
           complaints.add(record);
        }
        sheet.setRows(complaints);
        sheet.setTotal(complaints.size());
        return new RetResult<>(sheet);
    }

    @RestMapping(auth = true, actionid = ACTION_QUERY, comment = "查询举报详情")
    public RetResult<Sheet<ComplaintRecord>> findComplaintDetail(int targetid, Flipper flipper) {
        FilterNode filter = FilterNode.create("targetid", FilterExpress.EQUAL, targetid);
        Sheet<ComplaintRecord> sheet = platfSource.querySheet(ComplaintRecord.class, flipper, filter);
        sheet.forEach(complaint -> complaint.setComplaintusername(userService.findUserName(complaint.getComplaintuserid())));
        return new RetResult<>(sheet);
    }
    
}
